﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;
using System.IO;

namespace Waker
{
    public partial class FormLogin : Base.FormBase
    {
        public FormLogin()
        {
            InitializeComponent();
        }
        private void FormLogin_Load(object sender, EventArgs e)
        {
            var ini = new INIHelper(Consts.IniConfigPath);
            var userCode = ini.ReadValue("User", "UserCode");
            if (!string.IsNullOrEmpty(userCode))
            {
                txtUserCode.Text = userCode;
            }
            var isRemember = false;
            if (bool.TryParse(ini.ReadValue("User", "RememberUserCode"), out isRemember))
            {
                chkUserName.Checked = isRemember;
            }
            this.txtHardware.Text = Util.GetHardwareInfo();
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(txtUserCode.Text))
            {
                dxErrorProvider1.SetError(txtUserCode, "用户名不能为空");
                return;
            }
            var res = new BAL.BAL_SystemSetting().Login(txtUserCode.Text, txtPassword.Text, txtHardware.Text);
            switch (res)
            {
                case Entitys.Enums.EnumLoginResult.Success:

                    var ini = new INIHelper(Consts.IniConfigPath);
                    ini.Writue("User", "UserCode", chkUserName.Checked ? txtUserCode.Text : string.Empty);
                    ini.Writue("User", "RememberUserCode", chkUserName.Checked ? "true" : "false");

                    this.DialogResult = DialogResult.OK;
                    break;
                case Entitys.Enums.EnumLoginResult.AuthenticationFailed:
                    //UI.ShowError("机器未授权", "登陆失败");
                    dxErrorProvider1.SetError(txtHardware, "登陆失败,机器未授权");
                    break;
                default:
                case Entitys.Enums.EnumLoginResult.IncorrectUserIdOrPassword:
                    //UI.ShowError("用户名或密码不正确", "登陆失败");
                    dxErrorProvider1.SetError(txtPassword, "登陆失败,用户名或密码不正确");
                    txtPassword.Text = string.Empty;
                    break;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            //Close();
        }


    }
}
